/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package org.jastrzab.flashlight;

import android.app.PendingIntent;
import android.appwidget.AppWidgetManager;
import android.appwidget.AppWidgetProvider;
import android.content.ComponentName;
import android.content.Context;
import android.content.Intent;
import android.widget.RemoteViews;

/**
 *
 * @author Jastrzab
 */
public final class Switcher extends AppWidgetProvider {

	@Override
	public void onDisabled(Context context) {
		context.stopService(Intents.getService(context, null));
	}

	@Override
	public void onReceive(Context context, Intent intent) {
		if (intent.getAction().equals(Intents.UPDATE) || intent.getAction().equals(Intents.NOTIFY)) {
			AppWidgetManager appWidgetManager = AppWidgetManager.getInstance(context);
			ComponentName thisAppWidget = new ComponentName(context.getPackageName(), Switcher.class.getName());
			RemoteViews views = new RemoteViews(context.getPackageName(), R.layout.widget);
			views.setOnClickPendingIntent(R.id.switcher, PendingIntent.getService(context, 0, Intents.getService(context, Intents.WIDGET), 0));
			if (Light.isWorking()) {
				views.setImageViewResource(R.id.indicator, R.drawable.widget);
			} else {
				views.setImageViewResource(R.id.indicator, R.drawable.widget_off);
			}
			appWidgetManager.updateAppWidget(appWidgetManager.getAppWidgetIds(thisAppWidget), views);
		}
	}

}
